#include "../include/ftp.h"
#include <time.h>
#include <crypt.h>
#include <openssl/md5.h>
#define LENSALT 8
int genSalt(char *buf) {
    int flag, i;
    char* string;
    srand((unsigned) time(NULL ));
    if ((string = (char*) malloc(LENSALT)) == NULL ) {
        printf("gensalt malloc failed\n");
        return -1 ;
    }
    for (i = 0; i < LENSALT+1; i++) {
        flag = rand() % 3;
        switch (flag) {
        case 0: string[i] = 'A' + rand() % 26; break;
        case 1: string[i] = 'a' + rand() % 26; break;
        case 2: string[i] = '0' + rand() % 10; break;
        default: string[i] = 'x'; break;
        }
    }
    string[LENSALT] = '\0';
    strcpy(buf, string);
    free(string);
    string = NULL;
    return 0;
}
//int main(){
//    char buf[10] = {0};
//    strcpy(buf,"qOrvsN41");
//    //char a[20] = "";
//    genSalt(buf);
//    char salt[15] = {0};
//
//    sprintf(salt,"$6$%s",buf);
//    printf("salt = %s\n",salt);
//    char pwd[20] ={0};
//    strcpy(pwd,"1234");
//    char pwdk[100];
//
//    //char *p= crypt(pwd,buf);
//    //puts(p);
//    memcpy(pwdk, crypt("l",salt),99);
//    puts(pwdk);
//    int len = strlen(crypt(pwd,buf));
//    len = strlen(pwdk);
//    printf("%d\n",len);
  //  int ret = strcmp("$6$qOrvsN41$gGlv8z7P1sAKuyaTAY03AvCn9/Z6Ygc4DJ9Uwe0RVzNAI6TQsTfsdihPnIh3lSZV2C02HjW.9bvJNdep3k.ER.",pwdk);
  //  printf("%d\n",ret);
  //  int ret2 =strlen("gGlv8z7P1sAKuyaTAY03AvCn9/Z6Ygc4DJ9Uwe0RVzNAI6TQsTfsdihPnIh3lSZV2C02HjW.9bvJNdep3k.ER.");
  //nsert into account values(1,'root','$6$chQG8M71','$6$chQG8M71$akmxKztqfqmDINy3mVTtOb1iTYZnlbJwWwyGzhBOlTKYWhTzNAhio1asuzxDEeF1wvrwKCge2wXI14iq/3cGJ.', '1234')
  //printf("%d\n",ret2);
//}
